<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2016/1/22
 * Time: 15:18
 */

namespace app\controllers;

use app\models\WxGoodDao;
use Yii;
use app\lib\WXBaseController;
use app\lib\WeixinUtils;
use app\models\BagDao;
use app\models\FxDao;
use app\models\weixin\T_WXMALL_DORACARD;

use app\models\weixin\T_WXMALL_USER;

class WxPayController extends WXBaseController
{

    public function beforeAction($action)
    {
        $userinfo = WeixinUtils::weixin_userinfo_filter();
        if ($userinfo == null) {
            echo "验证失败";
            return false;
        }
        return true;
    }
    //wx-biz
    //订单确认
    public function actionWxCardOrderForm($order_code)
    {
        $session = Yii::$app->session;
        $user_id = $session->get(static::sessionKey)->userid;

        $orderInfo = WxBizController::___verifyOrder($order_code, $user_id);

        $STATUS = $orderInfo['STATUS'];
//        1：已购买未支付
//        2：已支付
//        3：已失效

        if ($STATUS != '1') {
            if ($STATUS == '2') {
                //已支付
                header("Content-Type:text/html;charset=utf-8");
                echo "已支付;STATUS:" . $STATUS;
                exit;
            } else {
                echo "已失效;STATUS:" . $STATUS;
                exit;
            }
        }
        $card = T_WXMALL_DORACARD::get_cardByCardId($orderInfo['CARD_ID']);
        if (count($card) == 0) {
            echo "此卡片不存在！";
            exit;
        }
        $card = $card[0];
        $user_info = T_WXMALL_USER::getUserByUserid($user_id);
        $user_info = $user_info[0];

        $re = FxDao::get_CurrentIndex($user_id);
        $discount = $re[0]['DISCOUNT'];
        return $this->render("@app/views/wx-biz/order-submit", ['card' => $card, 'orderInfo' => $orderInfo,
            'user_info' => $user_info, 'discount' => $discount,]);

    }
    //wx-bag
    //订单确认  code 订单编号
    public function actionWxBagOrderForm($code)
    {
        $session = Yii::$app->session;
        $user_id = $session->get(static::sessionKey)->userid;
        $orderInfo = WxBagController::___verifyOrder($code, $user_id);
        $STATUS = $orderInfo['STATUS'];
//        1：已购买未支付
//        2：已支付
//        3：已失效

        if ($STATUS != '1') {
            if ($STATUS == '2') {
                //已支付
                header("Content-Type:text/html;charset=utf-8");
                echo "已支付;STATUS:" . $STATUS;
                exit;
            } else {
                echo "已失效;STATUS:" . $STATUS;
                exit;
            }
        }

        $GoodsList = BagDao::getOrderGoodsListByOrderCode($code);

        $user_info = T_WXMALL_USER::getUserByUserid($user_id);
        $user_info = $user_info[0];

        $re = FxDao::get_CurrentIndex($user_id);
        $discount = $re[0]['DISCOUNT'];
        return $this->render("@app/views/wx-bag/order-submit", ['orderInfo' => $orderInfo, 'GoodsList' => $GoodsList,
            'user_info' => $user_info, 'discount' => $discount]);
    }

    //订单详情Frings
    public function actionOrderDetail($order_id)
    {
        static::__validUser();
        $currentUserId = static::getSessionUserid();

        $user_info = T_WXMALL_USER::getUserByUserid($currentUserId);
        $user_info = $user_info[0];
        $userDoraCoin = $user_info['DORACOIN_ACCOUNT'];// User::getUserIntegral($currentUserId);


        $order = WxGoodDao::getOrderByIdAndUserId($order_id, $currentUserId);
        $order["status_text"] = WxGoodDao::getOrderStatusText($order["STATUS"]);


        $goodsList = WxGoodDao::getOrderGoodsDetail($order_id);
        $virtualCodeList = null;
        if ($order['STATUS'] == 3) {
            $virtualCodeList = WxGoodDao::getVirtualCodeListByOrderId($order_id);
        }

        return $this->render('@app/views/wx-good/order-detail',
            [
                'order' => $order,
                'goodsList' => $goodsList,
                'userDoraCoin' => $userDoraCoin,
                'virtualCodeList' => $virtualCodeList
            ]);
    }
}